package leetcode_401_500;

public class LeeCode_434 {
    public static void main(String[] args) {
        System.out.println(countSegments(", , , ,        a, eaefa"));
    }
    private static int countSegments(String s) {
        int st = 0;
        while (st < s.length() && s.charAt(st) == ' '){
            st++;
        }
        int en = s.length() - 1;
        while (en >= 0 && s.charAt(en) == ' '){
            en--;
        }
        if (en < st){
            return 0;
        }else {
            s = s.substring(st, en + 1);
        }
        String[] s1 = s.split(" ");
        int ans = 0;
        for (String s2 : s1) {
            if (s2.length() != 0){
                ans++;
            }
        }
        return ans;
    }
}
